package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.DrugInfoMapper;
import com.ssy.bus.pojo.DrugInfo;
import com.ssy.bus.pojo.Production;
import com.ssy.bus.service.IDrugInfoService;
import com.ssy.bus.service.IProductionService;
import com.ssy.bus.vo.DrugInfoVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.CommonType;
import com.ssy.system.service.ICommonTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author ssy
 */
@Service
public class DrugInfoServiceImpl extends ServiceImpl<DrugInfoMapper, DrugInfo> implements IDrugInfoService {

    @Autowired
    private DrugInfoMapper drugInfoMapper;

    @Autowired
    private IProductionService productionService;

    @Autowired
    private ICommonTypeService commonTypeService;

    @Override
    public PageResult loadAllDrugInfoPage(DrugInfoVo drugInfoVo) {
        Page<DrugInfo> page = new Page<>(drugInfoVo.getPage(), drugInfoVo.getLimit());
        IPage<DrugInfo> drugInfoIPage = this.drugInfoMapper.selectPage(page, new LambdaQueryWrapper<DrugInfo>()
                .like(StringUtils.hasText(drugInfoVo.getName()), DrugInfo::getName, drugInfoVo.getName())
                .like(StringUtils.hasText(drugInfoVo.getCode()), DrugInfo::getCode, drugInfoVo.getCode())
                .eq(drugInfoVo.getDrugType() != null, DrugInfo::getDrugType, drugInfoVo.getDrugType())
                .eq(drugInfoVo.getProduction() != null, DrugInfo::getProduction, drugInfoVo.getProduction())
                .eq(drugInfoVo.getPreType() != null, DrugInfo::getPreType, drugInfoVo.getPreType())
                .eq(drugInfoVo.getState() != null, DrugInfo::getState, drugInfoVo.getState())
                .orderByDesc(DrugInfo::getCreateTime));

        List<DrugInfo> drugInfos = setDrugInfo(drugInfoIPage.getRecords());
        return new PageResult(drugInfoIPage.getTotal(), drugInfos);
    }

    @Override
    public PageResult loadMakeDrugInfoPage(DrugInfoVo druginfoVo) {
        Page<DrugInfo> page = new Page<>(druginfoVo.getPage(), druginfoVo.getLimit());
        IPage<DrugInfo> drugInfoIPage = this.drugInfoMapper.selectPage(page, new LambdaQueryWrapper<DrugInfo>()
                .like(StringUtils.hasText(druginfoVo.getCode()), DrugInfo::getCode, druginfoVo.getCode())
                .like(StringUtils.hasText(druginfoVo.getName()), DrugInfo::getName, druginfoVo.getName())
                .eq(druginfoVo.getDrugType() != null, DrugInfo::getDrugType, druginfoVo.getDrugType())
                .eq(druginfoVo.getPreType() != null, DrugInfo::getPreType, druginfoVo.getPreType())
                .eq(DrugInfo::getState, Constant.STATE));

        List<DrugInfo> drugInfos = setDrugInfo(drugInfoIPage.getRecords());
        return new PageResult(drugInfoIPage.getTotal(), drugInfos);
    }

    /**
     * @param drugInfoList 药品数据List
     * @return data
     */
    private List<DrugInfo> setDrugInfo(List<DrugInfo> drugInfoList) {
        for (DrugInfo record : drugInfoList) {

            //设置生产厂家名称
            Integer productionId = record.getProduction();
            Production production = this.productionService.selectOneById(productionId);
            record.setProductionName(production.getName());

            //设置药品类型名称
            Integer drugType = record.getDrugType();
            CommonType commonType = this.commonTypeService.queryCommonTypeById(drugType);
            record.setDrugTypeName(commonType.getName());

            //设置处方类型名称
            Integer preType = record.getPreType();
            CommonType commonType1 = this.commonTypeService.queryCommonTypeById(preType);
            record.setPreTypeName(commonType1.getName());
        }
        return drugInfoList;
    }

    @Override
    public Result insertDrugInfo(DrugInfo drugInfo) {
        if (drugInfo == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        drugInfo.setCreateTime(new Date());
        //drugInfo.setEarly(0);
        //drugInfo.setInventory(0);
        int i = this.drugInfoMapper.insert(drugInfo);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateDrugInfo(DrugInfo drugInfo) {
        if (drugInfo == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.drugInfoMapper.updateById(drugInfo);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteDrugInfoById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.drugInfoMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result bathDeleteDrugInfo(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        this.drugInfoMapper.deleteBatchIds(idList);
        return new Result(CodeMsg.DELETE_SUCCESS);
    }
}
